package com.cskaoyan.service;

import com.cskaoyan.bean.MarketAdmin;
import com.cskaoyan.bean.MarketAdminExample;
import com.cskaoyan.bean.MarketRole;
import com.cskaoyan.bean.MarketRoleExample;
import com.cskaoyan.bean.brand.OptionsVO;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketAdminMapper;
import com.cskaoyan.mapper.MarketRoleMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author stone
 * @date 2022/09/06 11:44
 */
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    MarketAdminMapper adminMapper;

    @Autowired
    MarketRoleMapper marketRoleMapper;

    @Override
    public MarketAdmin queryById(Integer id) {
        MarketAdmin marketAdmin = adminMapper.selectByPrimaryKey(id);
        return marketAdmin;
    }

    @Override
    public void modify(MarketAdmin admin) {
        adminMapper.updateByPrimaryKeySelective(admin);
    }

//    @Override
//    public List<MarketAdmin> query(String username, String password, String[] avatars,
//                                   Integer page, Integer limit) {
//        // 执行查询之前 → 使用PageHelper开启分页就可以了
//        PageHelper.startPage(page, limit);
//        MarketAdminExample example = new MarketAdminExample();
//        example.setDistinct(true);
//        example.setOrderByClause("username asc");
//        // List<Criterion> 向这个列表里塞入数据
//        // 条件查询
//
//        MarketAdminExample.Criteria criteria = example.createCriteria();//这个实例里封装了Criterion的列表
//        // 使用Example中的Criteria提供的方法，就可以构造Criterion实例，并且呢添加到list中
//        /*criteria.andUsernameEqualTo(username)
//                .andPasswordLike("%" + password + "%")
//                .andAvatarIn(Arrays.asList(avatars));*/
//        if (username != null && !"".equals(username)) {//StringUtils isEmpty
//            criteria.andUsernameEqualTo(username);
//        }
//        if (password != null && !"".equals(password)) {
//
//            criteria.andPasswordLike("%" + password + "%");
//        }
//        if (avatars != null && avatars.length > 0) {
//
//            criteria.andAvatarIn(Arrays.asList(avatars));
//        }
//        List<MarketAdmin> marketAdmins = adminMapper.selectByExample(example);
//        // page limit total
//        // 把mybatis查询出来的原始结果放进去
//        PageInfo pageInfo = new PageInfo(marketAdmins);
//        long total = pageInfo.getTotal();
//        return marketAdmins;
//    }

    @Override
    public CommonData<MarketAdmin> queryAdmin(MarketAdmin marketAdmin, BasePageInfo info) {
        PageHelper.startPage(info.getPage(),info.getLimit());

        MarketAdminExample marketAdminExample = new MarketAdminExample();
        Integer total = (int)adminMapper.countByExample(marketAdminExample);

        marketAdminExample.setOrderByClause(info.getSort() + " " + info.getOrder());

        MarketAdminExample.Criteria criteria = marketAdminExample.createCriteria();
        if (marketAdmin.getUsername() != null && !"".equals(marketAdmin.getUsername())) {
            criteria.andUsernameLike("%" + marketAdmin.getUsername() + "%");
        }
        List<MarketAdmin> marketAdmins = adminMapper.selectByExample(marketAdminExample);

        PageInfo<MarketAdmin> marketAdminPageInfo = new PageInfo<>(marketAdmins);
        CommonData data = CommonData.data(marketAdminPageInfo);
        data.setList(marketAdmins);
        data.setTotal(total);
        return data;
    }

    @Override
    public CommonData<MarketRole> queryRole() {
        MarketRoleExample marketRoleExample = new MarketRoleExample();


        List<MarketRole> marketRoles = marketRoleMapper.selectByExample(marketRoleExample);
        List<OptionsVO> optionsVOS = new ArrayList<>();
        for (MarketRole marketRole : marketRoles) {
            OptionsVO optionsVO = new OptionsVO();
            optionsVO.setValue(marketRole.getId());
            optionsVO.setLabel(marketRole.getName());
            optionsVOS.add(optionsVO);
        }
        PageInfo<MarketRole> marketRolePageInfo = new PageInfo<>(marketRoles);
        CommonData data = CommonData.data(marketRolePageInfo);
        data.setList(optionsVOS);
        return data;
    }

    @Override
    public MarketAdmin createAdmin(MarketAdmin marketAdmin) {
        adminMapper.insert(marketAdmin);
        return marketAdmin;
    }

    @Override
    public void deleteAdmin(MarketAdmin marketAdmin) {
        adminMapper.deleteByPrimaryKey(marketAdmin.getId());
        return;
    }

    /**
     * @author stone
     * @date 2022/09/06 11:44
     */
    public static interface AdminService {
        MarketAdmin queryById(Integer id);

        void modify(MarketAdmin admin);

        //List<MarketAdmin> query(String username, String password, String[] avatars,Integer page,Integer limit);

        CommonData<MarketRole> queryRole();

        CommonData<MarketAdmin> queryAdmin(MarketAdmin marketAdmin, BasePageInfo info);
    }
}
